package util;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class Druid {
    private Connection connection = null;
    private static DruidDataSource dataSource = new DruidDataSource();
    //加载驱动
    static {
         ResourceBundle mybd = ResourceBundle.getBundle("mybd");
         String driver = mybd.getString("driverclass");
         String url= mybd.getString("url");
         String userName = mybd.getString("userName");
         String passWord = mybd.getString("userPass");

        dataSource.setUsername(userName);
        dataSource.setUrl(url);
        dataSource.setPassword(passWord);
        dataSource.setDriverClassName(driver);
    }


    public Connection getConnection(){

        try {
            connection = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;

    }
    public void close(Connection connection, ResultSet resultSet, PreparedStatement preparedStatement){
        try {
            if (connection != null){
                connection.close();
            }
            if (resultSet != null){
                resultSet.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
